package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.HatchetMerchant;
import com.ruoyi.system.domain.HatchetMerchantIncreaseLimit;
import com.ruoyi.system.domain.HatchetOrderPayment;
import com.ruoyi.system.domain.HatchetPlan;
import com.ruoyi.system.domain.HatchetPlanItem;
import com.ruoyi.system.service.IHatchetMerchantIncreaseLimitService;
import com.ruoyi.system.service.IHatchetMerchantService;
import com.ruoyi.system.service.IHatchetOrderPaymentService;
import com.ruoyi.system.service.IHatchetPlanItemService;
import com.ruoyi.system.service.IHatchetPlanService;

/**
 * 还款计划明细 信息操作处理(代理商操作)
 * 
 * @author ruoyi
 * @date 2019-03-02
 */
@Controller		
@RequestMapping("/system/hatchetPlanItemForDLS")
public class HatchetPlanItemForDLSController extends BaseController
{
    private String prefix = "system/hatchetPlanItem";
	
	@Autowired
	private IHatchetPlanItemService hatchetPlanItemService;
	@Autowired
	private IHatchetMerchantIncreaseLimitService increaseLimitService;
	@Autowired
	private IHatchetMerchantService hatchetMerchantService;
	@Autowired
	private IHatchetPlanService planService;
	@Autowired
	private IHatchetOrderPaymentService orderPaymentService;
	@RequiresPermissions("system:hatchetPlanItem:view")
	@GetMapping()
	public String hatchetPlanItem()
	{
	    return prefix + "/hatchetPlanItemForDLS";
	}
	@GetMapping("/JLR")
	public String hatchetPlanItemForJLR( String planId, ModelMap map)
	{
		HatchetPlan plan = new HatchetPlan();
		plan.setID(planId);
		map.put("plan", plan);
	    return prefix + "/hatchetPlanItemForJLR";
	}
	
	@GetMapping("/JLRAll")
	public String hatchetPlanItemForJLRAll( String planId, ModelMap map)
	{
	    return prefix + "/hatchetPlanItemForJLRAll";
	}
	/**
	 * 查询还款计划明细列表
	 */
	@RequiresPermissions("system:hatchetPlanItem:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(HatchetPlanItem hatchetPlanItem)
	{
		List<String> merchantIdList = new ArrayList<>();
		List<HatchetPlanItem> list = new ArrayList<>();
		String loginName = getLoginName();
		//判断商户条件是否为空
		if (StringUtils.isNotEmpty(hatchetPlanItem.getMerchantName())
				||StringUtils.isNotEmpty(hatchetPlanItem.getMerchantNo())) {
			HatchetMerchant paramMerchant = new HatchetMerchant();
			paramMerchant.setMerchantCnName(hatchetPlanItem.getMerchantName());
			paramMerchant.setMerchantNo(hatchetPlanItem.getMerchantNo());
			List<HatchetMerchant> merchantList = hatchetMerchantService.selectHatchetMerchantList(paramMerchant);
			if (merchantList != null && merchantList.size()>0) {
				HatchetMerchant hatchetMerchant = merchantList.get(0);
				hatchetPlanItem.setMerchantId(hatchetMerchant.getID());
			}else {
				return getDataTable(new ArrayList<>());
			}
			//如果没有指定商户名称或者编号并且大计划id也为空 用循环查询所有商户
		}
		//查询高级顾问
		if (loginName.length()>8) {
			HatchetMerchant merchant = hatchetMerchantService.selectHatchetMerchantByNo(loginName);
			//连表查询明细
			hatchetPlanItem.setBrokerId(merchant.getID());
			startPage();
			list = hatchetPlanItemService.selectHatchetPlanItemListForJLR(hatchetPlanItem);
			
		//代理商	查询
		} else {
			merchantIdList = getMerchantIdList();
			//没有商户
			if (StringUtils.isEmpty(merchantIdList)) {
				return getDataTable(new ArrayList<>()); 
			}
			hatchetPlanItem.setMerchantIds(merchantIdList);
		    startPage();
	       list = hatchetPlanItemService.selectHatchetPlanItemListForDLS(hatchetPlanItem);
		}
        for (HatchetPlanItem hatchetPlanItem2 : list) {
        	//1.封装商户信息
        	HatchetMerchant merchant = hatchetMerchantService.selectHatchetMerchantById(hatchetPlanItem2.getMerchantId());
        	if (StringUtils.isNotNull(merchant)) {
        		hatchetPlanItem2.setMerchantNo(merchant.getMerchantNo()); 
        		hatchetPlanItem2.setMerchantName(merchant.getMerchantCnName());
			}
        	//2.封装账户信息
        	HatchetMerchantIncreaseLimit increaseLimit= 
        			increaseLimitService.selectHatchetMerchantIncreaseLimitById(hatchetPlanItem2.getFromIncreaseId());
        	if (StringUtils.isNotNull(increaseLimit)) {
        		if (StringUtils.isNotEmpty(increaseLimit.getBankAccount())) {
        			hatchetPlanItem2.setFromBankAccount(StringUtils.idCardNoString(increaseLimit.getBankAccount(),4));
				}
        		hatchetPlanItem2.setFromBankAccountName(increaseLimit.getBankAccountName());
        		if (StringUtils.isNotEmpty(increaseLimit.getBankPhone())) {
        			hatchetPlanItem2.setFromBankphone(StringUtils.phoneString(increaseLimit.getBankPhone()));
        			
				}
        		
			}
        	//3匹配通道返回信息
        	if ("10B".equals(hatchetPlanItem2.getSTATUS()) || "10C".equals(hatchetPlanItem2.getSTATUS())) {
        		if(StringUtils.isNotEmpty(hatchetPlanItem2.getOrderId())) {
            		HatchetOrderPayment orderPayment = orderPaymentService.selectHatchetOrderPaymentByOrder(hatchetPlanItem2.getOrderId());
            		if (StringUtils.isNotNull(orderPayment)) {
            			hatchetPlanItem2.setAcqResponseCode(orderPayment.getAcqResponseCode());
					}
        		}
			}
		}
        return getDataTable(list);
	}
	/**
	 * 查询还款计划明细列表
	 */
	@RequiresPermissions("system:hatchetPlanItem:list")
	@PostMapping("/listForJLR")
	@ResponseBody
	public TableDataInfo listForJLR(HatchetPlanItem hatchetPlanItem)
	{
		if (StringUtils.isEmpty(hatchetPlanItem.getPlanId())) {
			return getDataTable(new ArrayList<>());
		}
		startPage();
        List<HatchetPlanItem> list = hatchetPlanItemService.selectHatchetPlanItemList(hatchetPlanItem);
        for (HatchetPlanItem hatchetPlanItem2 : list) {
        	//1.封装商户信息
        	HatchetMerchant merchant = hatchetMerchantService.selectHatchetMerchantById(hatchetPlanItem2.getMerchantId());
        	if (StringUtils.isNotNull(merchant)) {
        		hatchetPlanItem2.setMerchantNo(merchant.getMerchantNo()); 
        		hatchetPlanItem2.setMerchantName(merchant.getMerchantCnName());
			}
        	//2.封装账户信息
        	HatchetMerchantIncreaseLimit increaseLimit= 
        			increaseLimitService.selectHatchetMerchantIncreaseLimitById(hatchetPlanItem2.getFromIncreaseId());
        	if (StringUtils.isNotNull(increaseLimit)) {
        		if (StringUtils.isNotEmpty(increaseLimit.getBankAccount())) {
        			hatchetPlanItem2.setFromBankAccount(StringUtils.idCardNoString(increaseLimit.getBankAccount(),4));
				}
        		hatchetPlanItem2.setFromBankAccountName(increaseLimit.getBankAccountName());
        		if (StringUtils.isNotEmpty(increaseLimit.getBankPhone())) {
        			hatchetPlanItem2.setFromBankphone(StringUtils.phoneString(increaseLimit.getBankPhone()));
        			
				}
        		
			}
        	//3匹配通道返回信息
        	if ("10B".equals(hatchetPlanItem2.getSTATUS()) || "10C".equals(hatchetPlanItem2.getSTATUS())) {
        		if(StringUtils.isNotEmpty(hatchetPlanItem2.getOrderId())) {
            		HatchetOrderPayment orderPayment = orderPaymentService.selectHatchetOrderPaymentByOrder(hatchetPlanItem2.getOrderId());
            		if (StringUtils.isNotNull(orderPayment)) {
            			hatchetPlanItem2.setAcqResponseCode(orderPayment.getAcqResponseCode());
					}
        		}
			}
		}
        return getDataTable(list);
	}
	/**
	 * 查询大计划执行进度
	 */
	@PostMapping("/getPlanProgress")
	@ResponseBody
	public Map<String, Object> getPlanProgress( HatchetPlanItem item)
	{
		 Map<String, Object> map = new HashMap<>();
		 double selectPlanProgress = 0l;
		 try {
		 selectPlanProgress = planService.selectPlanProgress(item.getPlanId());
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", BusinessStatus.FAIL);
			map.put("msg", "网络异常");
		}
		 map.put("status", BusinessStatus.SUCCESS);
		 map.put("progress", (int)(selectPlanProgress*100));
		 return map;
       
	}
}
